﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtJSExtender.Controls
{
    [Serializable]
    public class GridColumnCollection : ICollection, ICollection<GridColumn>
    {
        #region 属性

        private List<GridColumn> _list = null;

        #endregion

        #region 构造函数

        public GridColumnCollection()
        {
            this._list = new List<GridColumn>();
        }

        #endregion

        #region 公用方法

        public void Add(GridColumn column)
        {
            this._list.Add(column);
        }

        public GridColumn Add()
        {
            GridColumn column = new GridColumn();
            this.Add(column);
            return column;
        }

        public GridColumn Add(string HeaderText, string DataField)
        {
            GridColumn column = this.Add();
            column.HeaderText = HeaderText;
            column.DataField = DataField;

            return column;
        }

        public void AddRange(GridColumn[] columns)
        {
            this._list.AddRange(columns);
        }

        public int IndexOf(GridColumn column)
        {
            return this._list.IndexOf(column);
        }

        public int IndexOf(string HeaderText)
        {
            GridColumn column = this[HeaderText];
            if (column == null)
                return -1;

            return this.IndexOf(column);
        }

        public GridColumn this[string HeaderText]
        {
            get
            {
                foreach (GridColumn item in this._list)
                {
                    if (item.HeaderText == HeaderText)
                        return item;
                }

                return null;
            }
        }

        public GridColumn this[int index]
        {
            get
            {
                var obj = this._list[index];
                if (obj != null)
                    return (GridColumn)obj;

                return null;
            }
        }

        public void Remove(GridColumn column)
        {
            this._list.Remove(column);
        }

        public void Remove(string HeaderText)
        {
            GridColumn column = this[HeaderText];
            this._list.Remove(column);
        }

        public void RemoveAt(int index)
        {
            this._list.RemoveAt(index);
        }

        #endregion

        #region ICollection 成员

        public void CopyTo(Array array, int index)
        {
            this._list.ToArray().CopyTo(array, index);
        }

        public int Count
        {
            get { return this._list.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get
            {
                return this;
            }
        }

        #endregion

        #region IEnumerable 成员

        public IEnumerator GetEnumerator()
        {
            return this._list.GetEnumerator();
        }

        #endregion

        #region ICollection<GridColumn> 成员

        public void Clear()
        {
            this._list.Clear();
        }

        public bool Contains(GridColumn item)
        {
            return this._list.Contains(item);
        }

        public void CopyTo(GridColumn[] array, int arrayIndex)
        {
            this._list.CopyTo(array, arrayIndex);
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        bool ICollection<GridColumn>.Remove(GridColumn item)
        {
            return this._list.Remove(item);
        }

        #endregion

        #region IEnumerable<GridColumn> 成员

        IEnumerator<GridColumn> IEnumerable<GridColumn>.GetEnumerator()
        {
            return this._list.GetEnumerator();
        }

        #endregion
    }
}
